import { RequestUtil } from '@/util/ele_server_req';
export const clearObj=(obj)=>{
	for (const key in obj) {
	  delete obj[key]
	}
}
const getDictArray = async dictType => {
	let arr = [];
	await RequestUtil.do_get_async(
		'system/dict/data/listAll',
		{ dictType: dictType },
		response => {
			let count = response.data.total;
			if (count > 0) {
				Object.assign(arr, response.data.rows);
			}
		},
		error => {
			console.log(error);
		}
	);
	return arr;
};

const dict_type_name = {
	告警状态: 'alarm_status',
	告警类型: 'alarm_type',
	救援状态: 'rescure_status',
	救援类型: 'rescure_type',
	附件来源: 'mtn_attachment_source',
	梯内人员状态: 'ele_info_person_status',
	电梯门状态: 'ele_info_door_status',
	电源状态: 'ele_info_power_status',
	安全回路: 'ele_info_safety_circuit',
	电梯平稳度: 'ele_info_smoothness',
	紧急开关: 'ele_info_emergency_switch',
	轿厢运行状态: 'ele_info_work_status',
	服务模式: 'ele_info_service_mode',
	开锁区域: 'ele_info_unlocking_area',
	区域级别: 'ele_region_level', //省1市2区3街道4
	电梯品牌: 'ele_brand',
	电梯型号: 'ele_type',
	电梯类别: 'ele_class',
	监查单位: 'ele_inspector',
	公司类型: 'company_type',
	摄像头品牌: 'camera_brand',
	保险险种: 'ele_insure_type',
	电梯状态: 'ele_install_status',
	报修状态: 'ele_fix_proc',
	维修状态: 'ele_fix_status',
	报修来源: 'ele_fix_source',
	检测协议状态:'inspector_contract_status',
	检测协议缺省值:'inspector_default_value',
	检测电梯类别:'inspector_ele_type',
	检测电梯控制类型:'inspector_ele_controltype',
	电梯检测状态:'inspector_status',
	电梯细项检测状态:'chk_workitem_code'
};
//严重程度配色（6级）
const warn6_color_arr=['#78c807','#ffba0b','#ff8d0b','#f46b09','#ef3f09','#ea0000']

//所有用到的字典缓存
const dict_cache = {};

//把字典按中文:值的形式组织
const genernate_label_id_obj = arr => {
	let obj = {};
	for (let one of arr) {
		obj[one.dictLabel] = one.dictCode;
	}
	return obj;
};
const genernate_id_label_obj = arr => {
	let obj = {};
	for (let one of arr) {
		obj[one.dictCode] = one.dictLabel;
	}
	return obj;
};
//把字典按中文:值的形式组织
const genernate_label_value_obj = arr => {
	let obj = {};
	for (let one of arr) {
		obj[one.dictLabel] = one.dictValue;
	}
	return obj;
};
const genernate_value_label_obj = arr => {
	let obj = {};
	for (let one of arr) {
		obj[one.dictValue] = one.dictLabel;
	}
	return obj;
};
//查询并加载字典对象，划分成3种值缓存。
const findDictByTypeFromCache = async (dictType, refresh = false) => {
	if (dict_cache.hasOwnProperty(dictType)) {
		if (refresh == true) {
			delete dict_cache[dictType];
			return findDictByTypeFromCache(dictType, false);
		}
		return dict_cache[dictType];
	} else {
		let arr = await getDictArray(dictType);
		if (arr.length > 0) {
			const one = {
				arr: arr,
				label_id: genernate_label_id_obj(arr),
				id_label: genernate_id_label_obj(arr),
				label_value: genernate_label_value_obj(arr),
				value_label: genernate_value_label_obj(arr)
			};
			dict_cache[dictType] = one;
			return one;
		} else {
			return undefined;
		}
	}
};
const findDictLabelById = async (dictType, id, refresh = false) => {
	const dictObj = await findDictByTypeFromCache(dictType, refresh);
	if (dictObj) {
		return dictObj.id_label[id];
	} else {
		return undefined;
	}
};
const findDictIdByLabel = async (dictType, label, refresh = false) => {
	const dictObj = await findDictByTypeFromCache(dictType, refresh);
	if (dictObj) {
		return dictObj.label_id[label];
	} else {
		return undefined;
	}
};
const findDictLabelByValue = async (dictType, value, refresh = false) => {
	const dictObj = await findDictByTypeFromCache(dictType, refresh);
	if (dictObj) {
		return dictObj.value_label[value];
	} else {
		return undefined;
	}
};
const findDictValueByLabel = async (dictType, label, refresh = false) => {
	const dictObj = await findDictByTypeFromCache(dictType, refresh);
	if (dictObj) {
		return dictObj.label_value[label];
	} else {
		return undefined;
	}
};
const findDictArray = async (dictType, refresh = false) => {
	const dictObj = await findDictByTypeFromCache(dictType, refresh);
	if (dictObj) {
		return dictObj.arr;
	} else {
		return undefined;
	}
};

export const SysDictConst = {
	Const_Yes: '1',
	Const_No: '2',
	Const_ele_fix_proc: {
		//报修状态
		findStatusValue: async label => {
			return await findDictValueByLabel('ele_fix_proc', label);
		},
		//报修状态
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('ele_fix_proc', value);
			return label
		},
		
	},
	Const_ele_fix_status: {
		//报修状态
		findStatusValue: async label => {
			return await findDictValueByLabel('ele_fix_status', label);
		},
		//报修状态
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('ele_fix_status', value);
			return label
		},
	},
	Const_contract_type: {
		//合同状态
		findDictList: async () => {
			return await findDictArray('contract_type');
		},
		findTypeNameByValue: async value => {
			let label=await findDictLabelByValue('contract_type', value);
			return label
		},
	},
	Const_mtn_serv_status: {
		//维保服务状态
		findStatusValue: async label => {
			return await findDictValueByLabel('mtn_serv_status', label);
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('mtn_serv_status', value);
			return label
		},
		
	},
	Const_sys_job_status: {
		//维保任务状态
		findStatusValue: async label => {
			return await findDictValueByLabel('sys_job_status', label);
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('sys_job_status', value);
			return label
		},
		get4RepairStatusColor:()=>{//维修状态的颜色
			return warn6_color_arr[5];//返回红色
		},
		getNormalStatusColor:()=>{//正常状态的颜色
			return warn6_color_arr[0];//返回绿色
		},
		getFixedStatusColor:()=>{//处理过恢复正常状态的颜色
			return warn6_color_arr[3];//返回橙色
		}
	},
	Const_mtn_serv_type: {
		//维保模板类型
		findStatusValue: async label => {
			return await findDictValueByLabel('mtn_serv_type', label);
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('mtn_serv_type', value);
			return label
		},
		
	},
	Const_camera_brand: {
		//摄像头品牌
		findStatusValue: async label => {
			return await findDictValueByLabel('camera_brand', label);
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('camera_brand', value);
			return label
		},
		
	},
	Const_alarm_code: {
		//电梯报警代码
		findDictList: async () => {
			return await findDictArray('alarm_code');
		},
		findAlarmNameByValue: async value => {
			let label=await findDictLabelByValue('alarm_code', value);
			return label
		},
		
	},
	Const_mtn_recorditem_status: {
		//维保条目状态
		findDictList: async () => {
			return await findDictArray('mtn_recorditem_status');
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('mtn_recorditem_status', value);
			return label
		},
		findColorByValue:value=>{
			const _idx=Number(value)
			return warn6_color_arr[_idx]
		}
	},
	Const_inspector_contract_status: {
		//检测协议状态
		findDictList: async () => {
			return await findDictArray('inspector_contract_status');
		},
		findStatusNameByValue: async value => {
			let label=await findDictLabelByValue('inspector_contract_status', value);
			return label
		},
		getSuccessStatusColor:()=>{//正常状态的颜色
			return warn6_color_arr[0];//返回绿色
		},
		getWarningStatusColor:(level)=>{//返回警告状态的颜色，表示该合同快要到期了
			if(level<1){level=1;}
			else if(level>4){level=4;}
			return warn6_color_arr[level];//返回橙色
		},
		getCloseStatusColor:()=>{//返回报错状态的颜色，表示该合同因其他原因关闭
			return warn6_color_arr[5];//返回红色
		},
	},
	Const_inspector_default_value: {
		//检测协议用到的缺省值
		findDefaultValueByLabel: async label => {
			return await findDictValueByLabel('inspector_default_value', label);
		},
	},
	Const_inspector_ele_type: {
		//检测电梯类别
		findDictList: async () => {
			return await findDictArray('inspector_ele_type');
		}
	},
	Const_inspector_ele_controltype: {
		//检测电梯类别
		findDictList: async () => {
			return await findDictArray('inspector_ele_controltype');
		}
	},
	Const_inspector_status: {
		//电梯检测状态
		findDictList: async () => {
			return await findDictArray('inspector_status');
		},
		findDictListInForm: async () => {
			//只保留填写表单中的选项
			const _arr= await findDictArray('inspector_status');
			let result=_arr.filter(item =>['2','3','4'].includes(item.dictValue));
			return result;
		},
		findStatusValue: async label => {
			return await findDictValueByLabel('inspector_status', label);
		},
		findStatusNameByValue: async value => {
			if(!value){
				value=0;
			}
			let label=await findDictLabelByValue('inspector_status', value);
			return label
		},
		getSuccessStatusColor:()=>{//正常状态的颜色
			return warn6_color_arr[0];//返回绿色
		},
		getWarningStatusColor:()=>{//返回警告状态的颜色，表示该合同快要到期了
			return warn6_color_arr[4];//返回橙色
		},
		getCloseStatusColor:()=>{//返回报错状态的颜色，表示该合同因其他原因关闭
			return warn6_color_arr[5];//返回红色
		}
	}
};
